<?php
import('ORG.Util.Function');
import('@.Org.CommonTool');
import('@.Org.FlowTool');
import('@.Org.GoodsTool');

class FlowAction extends CommonAction {
	
	public function _initialize(){
		parent::_initialize();
		//titile
		$this->assign("global_title",		"我的购物车");
	}	
	
	public function index(){
		$CommonModel				=	new CommonModel();
		$GoodsModel					=	new GoodsModel();
		//标记购物流程为普通商品
	    $_SESSION['flow_type'] 		=	CART_GENERAL_GOODS;	
	    //如果是一步购物，跳到结算中心
	    if ($GLOBALS['_CFG']['one_step_buy'] == '1'){
	        //ecs_header("Location: flow.php?step=checkout\n");
	        //exit;
	    }	
	    //取得商品列表,计算合计
	    $cart_goods 				= 	get_cart_goods();	
	    $list	=	$cart_goods["goods_list"];
		//titile
		$this->assign("global_title",		"购买" . $list[0]['goods_name'] . "_音伴尔官网");
			    
	    if(empty($cart_goods['goods_list'])){
	    	$msg_list 		=	build_tips_msg();
	    	$this->assign('msg_list',$msg_list);
	    	$this->display('msg_tips');
	    	exit();
	    }	    
	    $this->assign('goods_list', 		$cart_goods['goods_list']);	    
	    $this->assign('total', 				$cart_goods['total']);
	
	    //购物车的描述的格式化
	    $this->assign('shopping_money',     sprintf($GLOBALS['_LANG']['shopping_money'], $cart_goods['total']['goods_price']));
	    $this->assign('market_price_desc',  sprintf($GLOBALS['_LANG']['than_market_price'], $cart_goods['total']['market_price'], $cart_goods['total']['saving'], $cart_goods['total']['save_rate']));
		// 显示收藏夹内的商品
	    if ($_SESSION['user_id'] > 0){
	        $collection_goods 		= 	get_collection_goods($_SESSION['user_id']);
	        $this->assign('collection_goods',	$collection_goods);
	    }	
	    
	    //取得优惠活动
	    $favourable_list 			= 	favourable_list($_SESSION['user_rank']);
	    usort($favourable_list, 			'cmp_favourable');	
	    $this->assign('favourable_list', 	$favourable_list);
	
	    //计算折扣 
	    $discount 					= 	compute_discount();	    
	    $this->assign('discount', $discount['discount']);
	    $favour_name 				= 	empty($discount['name']) ? '' : join(',', $discount['name']);
	    $this->assign('your_discount', 	sprintf($GLOBALS['_LANG']['your_discount'], $favour_name, price_format($discount['discount'])));
	
	    //增加是否在购物车里显示商品图
	    $this->assign('show_goods_thumb', 		$GLOBALS['_CFG']['show_goods_in_cart']);	
	    //增加是否在购物车里显示商品属性
	    $this->assign('show_goods_attribute',	$GLOBALS['_CFG']['show_attr_in_cart']);
		
	    //购物车中商品配件列表
	    //1:取得购物车中基本件ID
	    $parent_list 				= 	$GoodsModel->get_fittings_parentid();
	    $fittings_list 				=	get_goods_fittings($parent_list);	    
	    
	    $this->assign('fittings_list', 		$fittings_list);
	    $this->assign('currency_format', 	$GLOBALS['_CFG']['currency_format']);
		$this->assign('integral_scale',  	$GLOBALS['_CFG']['integral_scale']);
		$this->assign('step',            	'cart');
		$this->display('flow');
	}
	
	/**
	 * 收获人地址
	 * Enter description here ...
	 */
	function consignee(){		
		$this->display('flow');	
	}
	
	/**
	 * 登陆界面
	 * Enter description here ...
	 */
	function login(){		
		$this->display('login');
	}
	
	/**
	 * 清除购物车
	 * Enter description here ...
	 */
	function clear(){
		$sql 		= 	"DELETE FROM " . 'ybr_cart' . " WHERE session_id='" . SESS_ID . "'";
    	M()->query($sql);
	}
	
	/**
	 * 删除购物车上的商品
	 * 
	 */
	public function drop_goods(){
		$rec_id 	= 	intval($_REQUEST['id']);		
		//删除商品
	    flow_drop_cart_goods($rec_id);
	    if($this->isAjax()){
	    	$this->ajaxReturn(1,"ok",1);
	    }
	    else{
	    	$this->success("成功删除购物车上的商品",U('flow/index'));
	    }
	}
		
	/**
	 * 订单确认
	 * Enter description here ...
	 */
	function checkout(){	
		$CommonModel	=	new CommonModel();
		$this->assign("step",'checkout');
	 	/* 获得用户所有的收货人信息 */
	    $consignee_list 			=	get_consignee_list($_SESSION['user_id']);   
	    	    
	    foreach($consignee_list as $k => $consignee){
	    	$consignee['key']		=	$k;
	    	$consignee_list[$k]		=	$consignee;
	    }	    
	    //if (count($consignee_list) < 5 && $_SESSION['user_id'] > 0){
	        ///* 如果用户收货人信息的总数小于5 则增加一个新的收货人信息 */
	        //$consignee_list[] 		= 	array('country' => $GLOBALS['_CFG']['shop_country'], 'email' => isset($_SESSION['email']) ? $_SESSION['email'] : '' ,'key'=>count($consignee_list) );
	    //}	    
	    /* 取得国家列表，如果有收货人列表，取得省市区列表 */
	    foreach ($consignee_list AS $region_id => $consignee){
	        //$consignee['country']  					= 	isset($consignee['country'])  ? intval($consignee['country'])  : 0;
	        $consignee['province'] 					= 	isset($consignee['province']) ? intval($consignee['province']) : 0;
	        $consignee['city']     					= 	isset($consignee['city'])     ? intval($consignee['city'])     : 0;
	        $consignee['district']     				= 	isset($consignee['district'])     ? intval($consignee['district'])     : 0;
			
	        //$consignee_list[$region_id]['country']	=	get_region_name($consignee['country']);
	        $consignee_list[$region_id]['province']	=	get_region_name($consignee['province']);
	        $consignee_list[$region_id]['city']		=	get_region_name($consignee['city']);
	        $consignee_list[$region_id]['district']	=	get_region_name($consignee['district']);
	    }	
	    /* 默认的收获地址 */
	    $default_consignee 							= 	get_consignee($_SESSION['user_id']);
	    $this->assign('consignee_list',			$consignee_list);
	    $this->assign('default_consignee'		,$default_consignee); // 默认
	    
		/* 添加新地址需要的省份列表  */
		$province_list								=	get_regions(1,1);
		$this->assign('province_list',$province_list);
		
	    /* 取得购物类型 */
	    $flow_type 									= 	isset($_SESSION['flow_type']) ? intval($_SESSION['flow_type']) : CART_GENERAL_GOODS;
	
	    /* 团购标志 */
	    if ($flow_type == CART_GROUP_BUY_GOODS){
	        $this->assign('is_group_buy', 1);
	    }
	    /* 积分兑换商品 */
	    elseif ($flow_type == CART_EXCHANGE_GOODS){
	        $this->assign('is_exchange_goods', 1);
	    }
	    else{
	        //正常购物流程  清空其他购物流程情况
	        $_SESSION['flow_order']['extension_code'] = '';
	    }
	
	    /* 检查购物车中是否有商品 */
	    $sql 			= 	"SELECT COUNT(*) FROM " . 'ybr_cart' .
					        " WHERE session_id = '" . SESS_ID . "' " .
					        "AND parent_id = 0 AND is_gift = 0 AND rec_type = '$flow_type'";
	
	    if ($CommonModel->getOne($sql) == 0){
	        $this->error($GLOBALS['_LANG']['no_goods_in_cart']);
	    }
	    
	    /*
	     * 检查用户是否已经登录
	     * 如果用户已经登录了则检查是否有默认的收货地址
	     * 如果没有登录则跳转到登录和注册页面
	     */
	    if (empty($_SESSION['direct_shopping']) && $_SESSION['user_id'] == 0){
	        /* 用户没有登录且没有选定匿名购物，转向到登录页面 */
	    	header('Location:'  . U('pass/login') . "?back_url=" . $_COOKIE['back_url'] );
	    }
	
	    $consignee 		= 	get_consignee($_SESSION['user_id']);
		
	    /* 检查收货人信息是否完整 */
	    if (!check_consignee_info($consignee, $flow_type)){
	        /* 如果不完整则转向到收货人信息填写界面
	        ecs_header("Location: flow.php?step=consignee\n");
	        exit; */
	    }
	    
	    $_SESSION['flow_consignee'] 	= 	$consignee;
	    $this->assign('consignee', $consignee);
	
	    /* 对商品信息赋值 */
	    $cart_goods 					= 	cart_goods($flow_type); // 取得商品列表，计算合计
	    $this->assign('goods_list', $cart_goods);
	
	    /* 对是否允许修改购物车赋值 */
	    if ($flow_type != CART_GENERAL_GOODS || $GLOBALS['_CFG']['one_step_buy'] == '1'){
	        $this->assign('allow_edit_cart', 0);
	    }
	    else{
	        $this->assign('allow_edit_cart', 1);
	    }
	
	    /*
	     * 取得购物流程设置
	     */	    
	    $this->assign('config', $GLOBALS['_CFG']);
	    /*
	     * 取得订单信息
	     */
	    $order 							= 	flow_order_info();
	    $this->assign('order', $order);
	
	    /* 计算折扣 */
	    if ($flow_type != CART_EXCHANGE_GOODS && $flow_type != CART_GROUP_BUY_GOODS){
	        $discount 					= 	compute_discount();
	        $this->assign('discount', $discount['discount']);
	        $favour_name 				= 	empty($discount['name']) ? '' : join(',', $discount['name']);
	        $this->assign('your_discount', sprintf($GLOBALS['_LANG']['your_discount'], $favour_name, price_format($discount['discount'])));
	    }
	
	    /*
	     * 计算订单的费用
	     */
	    $total 							= 	order_fee($order, $cart_goods, $consignee);
	
	    $this->assign('total', $total);
	    $this->assign('shopping_money', sprintf($GLOBALS['_LANG']['shopping_money'], $total['formated_goods_price']));
	    $this->assign('market_price_desc', sprintf($GLOBALS['_LANG']['than_market_price'], $total['formated_market_price'], $total['formated_saving'], $total['save_rate']));
	
	    /* 取得配送列表 */
	    $region            				= 	array($consignee['country'], $consignee['province'], $consignee['city'], $consignee['district']);
	    $shipping_list     				= 	available_shipping_list($region);
	    $cart_weight_price 				= 	cart_weight_price($flow_type);
	    $insure_disabled   				= 	true;
	    $cod_disabled      				= 	true;
		
	    // 查看购物车中是否全为免运费商品，若是则把运费赋为零
	    $sql 							= 	'SELECT count(*) FROM ' . 'ybr_cart' . " WHERE `session_id` = '" . SESS_ID. "' AND `extension_code` != 'package_buy' AND `is_shipping` = 0";
	    $shipping_count 				= 	$CommonModel->getOne($sql);
	
	    foreach ($shipping_list AS $key => $val){
	        $shipping_cfg 				= 	unserialize_config($val['configure']);
	        $shipping_fee 				= 	($shipping_count == 0 AND $cart_weight_price['free_shipping'] == 1) ? 0 : shipping_fee($val['shipping_code'], unserialize($val['configure']),
	        								$cart_weight_price['weight'], $cart_weight_price['amount'], $cart_weight_price['number']);
	
	        $shipping_list[$key]['format_shipping_fee'] = 	price_format($shipping_fee, false);
	        $shipping_list[$key]['shipping_fee']        = 	$shipping_fee;
	        $shipping_list[$key]['free_money']          = 	price_format($shipping_cfg['free_money'], false);
	        $shipping_list[$key]['insure_formated']     = 	strpos($val['insure'], '%') === false ? price_format($val['insure'], false) : $val['insure'];
	
	        /* 当前的配送方式是否支持保价 */
	        if ($val['shipping_id'] == $order['shipping_id']){
	            $insure_disabled 		= 	($val['insure'] == 0);
	            $cod_disabled    		= 	($val['support_cod'] == 0);
	        }
	    }
	
	    $this->assign('shipping_list',   	$shipping_list);
	    $this->assign('insure_disabled', 	$insure_disabled);
	    $this->assign('cod_disabled',    	$cod_disabled);
	
	    /* 取得支付列表 */
	    if ($order['shipping_id'] == 0){
	        $cod        				= 	true;
	        $cod_fee    				= 	0;
	    }
	    else{
	        $shipping 					=	shipping_info($order['shipping_id']);
	        $cod 						= 	$shipping['support_cod'];
			
	        if ($cod){
	            /* 如果是团购，且保证金大于0，不能使用货到付款 */
	            if ($flow_type == CART_GROUP_BUY_GOODS){
	                $group_buy_id 		= 	$_SESSION['extension_id'];
	                if ($group_buy_id <= 0){
	                    //show_message('error group_buy_id');
	                    $this->error('group_buy_id');
	                }
	                $group_buy 			= 	group_buy_info($group_buy_id);
	                if (empty($group_buy)){
	                    $this->error('group buy not exists: ' . $group_buy_id);
	                }
	
	                if ($group_buy['deposit'] > 0){
	                    $cod 			= 	false;
	                    $cod_fee 		= 	0;	
	                    /* 赋值保证金 */
	                    $this->assign('gb_deposit', $group_buy['deposit']);
	                }
	            }
	
	            if ($cod){
	                $shipping_area_info = 	shipping_area_info($order['shipping_id'], $region);
	                $cod_fee            = 	$shipping_area_info['pay_fee'];
	            }
	        }
	        else{
	            $cod_fee 				= 	0;
	        }
	    }
	
	    // 给货到付款的手续费加<span id>，以便改变配送的时候动态显示
	    $payment_list 					= 	available_payment_list(1, $cod_fee);
	    if(isset($payment_list)){
	        foreach ($payment_list as $key => $payment){
	            if ($payment['is_cod'] == '1'){
	                $payment_list[$key]['format_pay_fee'] 	= 	'<span id="ECS_CODFEE">' . $payment['format_pay_fee'] . '</span>';
	            }
	            /* 如果有易宝神州行支付 如果订单金额大于300 则不显示 */
	            if ($payment['pay_code'] == 'yeepayszx' && $total['amount'] > 300){
	                unset($payment_list[$key]);
	            }
	            /* 如果有余额支付 */
	            if ($payment['pay_code'] == 'balance'){
	                /* 如果未登录，不显示 */
	                if ($_SESSION['user_id'] == 0){
	                    unset($payment_list[$key]);
	                }
	                else{
	                    if ($_SESSION['flow_order']['pay_id'] == $payment['pay_id']){
	                        $this->assign('disable_surplus', 1);
	                    }
	                }
	            }
	        }
	    }
	    	    
	    //$this->assign('payment_list', $payment_list);
	
	    /* 取得包装与贺卡 */
	    if ($total['real_goods_count'] > 0){
	        /* 只有有实体商品,才要判断包装和贺卡 */
	        if (!isset($GLOBALS['_CFG']['use_package']) || $GLOBALS['_CFG']['use_package'] == '1'){
	            /* 如果使用包装，取得包装列表及用户选择的包装 */
	            $this->assign('pack_list', pack_list());
	        }
	
	        /* 如果使用贺卡，取得贺卡列表及用户选择的贺卡 */
	        if (!isset($GLOBALS['_CFG']['use_card']) || $GLOBALS['_CFG']['use_card'] == '1'){
	            $this->assign('card_list', card_list());
	        }
	    }
	
	    $user_info 			= 	user_info($_SESSION['user_id']);
	
	    /* 如果使用余额，取得用户余额 */
	    if ((!isset($GLOBALS['_CFG']['use_surplus']) || $GLOBALS['_CFG']['use_surplus'] == '1') && $_SESSION['user_id'] > 0 && $user_info['user_money'] > 0){
	        // 能使用余额
	        $this->assign('allow_use_surplus', 1);
	        $this->assign('your_surplus', $user_info['user_money']);
	    }
	
	    /* 如果使用积分，取得用户可用积分及本订单最多可以使用的积分 */
	    if ((!isset($GLOBALS['_CFG']['use_integral']) || $GLOBALS['_CFG']['use_integral'] == '1')
	    	&& $_SESSION['user_id'] > 0&& $user_info['pay_points'] > 0 
	    	&& ($flow_type != CART_GROUP_BUY_GOODS && $flow_type != CART_EXCHANGE_GOODS))
	    {
	        // 能使用积分
	        $this->assign('allow_use_integral', 1);
	        $this->assign('order_max_integral', flow_available_points());  // 可用积分
	        $this->assign('your_integral',      $user_info['pay_points']); // 用户积分
	    }
	
	    /* 如果使用红包，取得用户可以使用的红包及用户选择的红包 */
	    if ((!isset($GLOBALS['_CFG']['use_bonus']) || $GLOBALS['_CFG']['use_bonus'] == '1')
	        && ($flow_type != CART_GROUP_BUY_GOODS && $flow_type != CART_EXCHANGE_GOODS))
	    {
	        // 取得用户可用红包
	        $user_bonus 		= 	user_bonus($_SESSION['user_id'], $total['goods_price']);
	        if (!empty($user_bonus)){
	            foreach ($user_bonus AS $key => $val){
	                $user_bonus[$key]['bonus_money_formated'] = price_format($val['type_money'], false);
	            }
	            $this->assign('bonus_list', $user_bonus);
	        }
	
	        // 能使用红包
	        $this->assign('allow_use_bonus', 1);
	    }
	
	    /* 如果使用缺货处理，取得缺货处理列表 */
	    if (!isset($GLOBALS['_CFG']['use_how_oos']) || $GLOBALS['_CFG']['use_how_oos'] == '1'){
	        if (is_array($GLOBALS['_LANG']['oos']) && !empty($GLOBALS['_LANG']['oos'])){
	            $this->assign('how_oos_list', $GLOBALS['_LANG']['oos']);
	        }
	    }
	
	    /* 如果能开发票，取得发票内容列表 */
	    if ((!isset($GLOBALS['_CFG']['can_invoice']) || $GLOBALS['_CFG']['can_invoice'] == '1')
	        && isset($GLOBALS['_CFG']['invoice_content'])
	        && trim($GLOBALS['_CFG']['invoice_content']) != '' && $flow_type != CART_EXCHANGE_GOODS)
	    {
	        $inv_content_list 				= 	explode("\n", str_replace("\r", '', $GLOBALS['_CFG']['invoice_content']));
	        $this->assign('inv_content_list', $inv_content_list);
	
	        $inv_type_list = array();
	        foreach ($GLOBALS['_CFG']['invoice_type']['type'] as $key => $type){
	            if (!empty($type)){
	                $inv_type_list[$type] 	= 	$type . ' [' . floatval($GLOBALS['_CFG']['invoice_type']['rate'][$key]) . '%]';
	            }
	        }
	        $this->assign('inv_type_list', $inv_type_list);
	    }
	
	    /* 保存 session */
	    $_SESSION['flow_order'] = $order;	    		
		$this->display('checkout');
	}
	
	/**
	 * 改变余额
	 * Enter description here ...
	 */
	public function change_surplus(){
	
	    $surplus   				= 	floatval($_GET['surplus']);
	    $user_info 				= 	user_info($_SESSION['user_id']);
	
	    if ($user_info['user_money'] + $user_info['credit_line'] < $surplus){
	        $result['error'] 	= 	$GLOBALS['_LANG']['surplus_not_enough'];
	    }
	    else{
	        /* 取得购物类型 */
	        $flow_type 			= 	isset($_SESSION['flow_type']) ? intval($_SESSION['flow_type']) : CART_GENERAL_GOODS;
	
	        /* 取得购物流程设置 */
	        $this->assign('config', $GLOBALS['_CFG']);
	
	        /* 获得收货人信息 */
	        $consignee 			= 	get_consignee($_SESSION['user_id']);
	
	        /* 对商品信息赋值 */
	        $cart_goods 		= 	cart_goods($flow_type); // 取得商品列表，计算合计
	
	        if (empty($cart_goods) || !check_consignee_info($consignee, $flow_type)){
	            $result['error'] 	= 	$GLOBALS['_LANG']['no_goods_in_cart'];
	        }
	        else{
	            /* 取得订单信息 */
	            $order 				= 	flow_order_info();
	            $order['surplus'] 	= 	$surplus;
	
	            /* 计算订单的费用 */
	            $total 				= 	order_fee($order, $cart_goods, $consignee);
	            $this->assign('total', $total);
	
	            /* 团购标志 */
	            if ($flow_type == CART_GROUP_BUY_GOODS){
	                $this->assign('is_group_buy', 1);
	            }
	
	            $result['content'] 	= 	$this->fetch('library/order_total.lbi');
	        }
	    }
	    
	    die($json->encode($result));
    
	}
	
	/**
	 * 检查用户输入的余额
	 * Enter description here ...
	 */
	public function check_surplus(){
	    $surplus   			= 	floatval($_GET['surplus']);
	    $user_info 			= 	user_info($_SESSION['user_id']);
	
	    if (($user_info['user_money'] + $user_info['credit_line'] < $surplus)){
	        die($GLOBALS['_LANG']['surplus_not_enough']);
	    }
	}
	
	/**
	 * 确认订单提交到数据库
	 * Enter description here ...
	 */
	function done(){		
			
		$CommonModel			=	new CommonModel();
		
		
//	    /* 取得购物类型 */
//	    $flow_type 				= 	isset($_SESSION['flow_type']) ? intval($_SESSION['flow_type']) : CART_GENERAL_GOODS;
//	
//	    /* 检查购物车中是否有商品 */
//	    $sql 					= 	"SELECT COUNT(*) FROM " . 'ybr_cart' .
//	        						" WHERE session_id = '" . SESS_ID . "' " .
//	        						"AND parent_id = 0 AND is_gift = 0 AND rec_type = '$flow_type'";
//	    if ($CommonModel->getOne($sql) == 0){
//	        $this->error($GLOBALS['_LANG']['no_goods_in_cart']);
//	    }
//	
//	    /* 检查商品库存 */
//	    /* 如果使用库存，且下订单时减库存，则减少库存 */
//	    if ($GLOBALS['_CFG']['use_storage'] == '1' && $GLOBALS['_CFG']['stock_dec_time'] == SDT_PLACE){
//	        $cart_goods_stock 			= 	get_cart_goods();
//	        $_cart_goods_stock 			= 	array();
//	        foreach ($cart_goods_stock['goods_list'] as $value){
//	            $_cart_goods_stock[$value['rec_id']] 	= 	$value['goods_number'];
//	        }
//	        flow_cart_stock($_cart_goods_stock);
//	        unset($cart_goods_stock, $_cart_goods_stock);
//	    }
//	
//	    /*
//	     * 检查用户是否已经登录
//	     * 如果用户已经登录了则检查是否有默认的收货地址
//	     * 如果没有登录则跳转到登录和注册页面
//	     */
//	    if (empty($_SESSION['direct_shopping']) && $_SESSION['user_id'] == 0){
//	        /* 用户没有登录且没有选定匿名购物，转向到登录页面 */
//	        //ecs_header();
//	        //exit;
//	        //$this->redirect('/pass/login');
//	        header("Location:" . U('pass/login') . "?back_url=http://localhost/shop/flow/checkout.html\n");
//	    }
//	
//	    $consignee = get_consignee($_SESSION['user_id']);
//	
//	    /* 检查收货人信息是否完整 */
//	    if (!check_consignee_info($consignee, $flow_type)){
//	        /* 如果不完整则转向到收货人信息填写界面 */
//	        //ecs_header("Location: flow.php?step=consignee\n");
//	        //exit;
//	        //$this->redirect('/flow/consignee');
//	        t("添加收获地址");
//	    }
//	
//	    $_POST['how_oos'] 			= 	isset($_POST['how_oos']) ? intval($_POST['how_oos']) : 0;
//	    $_POST['card_message'] 		= 	isset($_POST['card_message']) ? compile_str($_POST['card_message']) : '';
//	    $_POST['inv_type'] 			= 	!empty($_POST['inv_type']) ? compile_str($_POST['inv_type']) : '';
//	    $_POST['inv_payee'] 		= 	isset($_POST['inv_payee']) ? compile_str($_POST['inv_payee']) : '';
//	    $_POST['inv_content'] 		= 	isset($_POST['inv_content']) ? compile_str($_POST['inv_content']) : '';
//	    $_POST['postscript'] 		= 	isset($_POST['postscript']) ? compile_str($_POST['postscript']) : '';
//	
//	    $order = array(
//	        'shipping_id'     => intval($_POST['shipping']),
//	        'pay_id'          => intval($_POST['payment']),
//	        'pack_id'         => isset($_POST['pack']) ? intval($_POST['pack']) : 0,
//	        'card_id'         => isset($_POST['card']) ? intval($_POST['card']) : 0,
//	        'card_message'    => trim($_POST['card_message']),
//	        'surplus'         => isset($_POST['surplus']) ? floatval($_POST['surplus']) : 0.00,
//	        'integral'        => isset($_POST['integral']) ? intval($_POST['integral']) : 0,
//	        'bonus_id'        => isset($_POST['bonus']) ? intval($_POST['bonus']) : 0,
//	        'need_inv'        => empty($_POST['need_inv']) ? 0 : 1,
//	        'inv_type'        => $_POST['inv_type'],
//	        'inv_payee'       => trim($_POST['inv_payee']),
//	        'inv_content'     => $_POST['inv_content'],
//	        'postscript'      => trim($_POST['postscript']),
//	        'how_oos'         => isset($GLOBALS['_LANG']['oos'][$_POST['how_oos']]) ? addslashes($GLOBALS['_LANG']['oos'][$_POST['how_oos']]) : '',
//	        'need_insure'     => isset($_POST['need_insure']) ? intval($_POST['need_insure']) : 0,
//	        'user_id'         => $_SESSION['user_id'],
//	        'add_time'        => gmtime(),
//	        'order_status'    => OS_UNCONFIRMED,
//	        'shipping_status' => SS_UNSHIPPED,
//	        'pay_status'      => PS_UNPAYED,
//	        'agency_id'       => get_agency_by_regions(array($consignee['country'], $consignee['province'], $consignee['city'], $consignee['district']))
//	        );
//	
//	        
//	    /* 扩展信息 */
//	    if (isset($_SESSION['flow_type']) && intval($_SESSION['flow_type']) != CART_GENERAL_GOODS){
//	        $order['extension_code'] 	= 	$_SESSION['extension_code'];
//	        $order['extension_id'] 		= 	$_SESSION['extension_id'];
//	    }
//	    else{
//	        $order['extension_code'] 	= 	'';
//	        $order['extension_id'] 		= 	0;
//	    }
//	
//	    /* 检查积分余额是否合法 */
//	    $user_id 						= 	$_SESSION['user_id'];
//	    if ($user_id > 0){
//	        $user_info 					= 	user_info($user_id);
//	
//	        $order['surplus'] 			= 	min($order['surplus'], $user_info['user_money'] + $user_info['credit_line']);
//	        if ($order['surplus'] < 0){
//	            $order['surplus'] 		= 	0;
//	        }
//	
//	        // 查询用户有多少积分
//	        $flow_points 				= 	flow_available_points();  // 该订单允许使用的积分
//	        $user_points 				= 	$user_info['pay_points']; // 用户的积分总数
//	
//	        $order['integral'] 			= 	min($order['integral'], $user_points, $flow_points);
//	        if ($order['integral'] < 0){
//	            $order['integral'] 		= 	0;
//	        }
//	    }
//	    else{
//	        $order['surplus']  			= 	0;
//	        $order['integral'] 			= 	0;
//	    }
//	
//	    /* 检查红包是否存在 */
//	    if ($order['bonus_id'] > 0){
//	        $bonus 						= 	bonus_info($order['bonus_id']);
//	
//	        if (empty($bonus) || $bonus['user_id'] != $user_id || $bonus['order_id'] > 0 || $bonus['min_goods_amount'] > cart_amount(true, $flow_type)){
//	            $order['bonus_id'] 		= 	0;
//	        }
//	    }
//	    elseif (isset($_POST['bonus_sn'])){
//	        $bonus_sn 					= 	trim($_POST['bonus_sn']);
//	        $bonus 						= 	bonus_info(0, $bonus_sn);
//	        $now 						= 	gmtime();
//	        if (empty($bonus) || $bonus['user_id'] > 0 || $bonus['order_id'] > 0 || $bonus['min_goods_amount'] > cart_amount(true, $flow_type) || $now > $bonus['use_end_date'])
//	        {}
//	        else{
//	            if ($user_id > 0){
//	                $sql 				= 	"UPDATE " . 'ybr_user_bonus' . " SET user_id = '$user_id' WHERE bonus_id = '$bonus[bonus_id]' LIMIT 1";
//	                $CommonModel->query($sql);
//	            }
//	            $order['bonus_id'] 		= 	$bonus['bonus_id'];
//	            $order['bonus_sn'] 		= 	$bonus_sn;
//	        }
//	    }
//	
//	    /* 订单中的商品 */
//	    $cart_goods 					= 	cart_goods($flow_type);
//	    
//	
//	    if (empty($cart_goods)){
//	        $this->error($GLOBALS['_LANG']['no_goods_in_cart']);
//	    }
//	
//	    /* 检查商品总额是否达到最低限购金额 */
//	    if ($flow_type == CART_GENERAL_GOODS && cart_amount(true, CART_GENERAL_GOODS) < $GLOBALS['_CFG']['min_goods_amount']){
//	        $this->error( sprintf($GLOBALS['_LANG']['goods_amount_not_enough'], price_format($GLOBALS['_CFG']['min_goods_amount'], false)) );
//	    }
//	
//	    /* 收货人信息 */
//	    foreach ($consignee as $key => $value){
//	        $order[$key] 				= 	addslashes($value);
//	    }
//	
//	   /* 判断是不是实体商品 */
//	    foreach ($cart_goods AS $val){
//	        /* 统计实体商品的个数 */
//	        if ($val['is_real']){
//	            $is_real_good			=	1;
//	        }
//	    }
//	    if(isset($is_real_good)){
//	        $sql						=	"SELECT shipping_id FROM " . 'ybr_shipping' . " WHERE shipping_id=".$order['shipping_id'] ." AND enabled =1"; 
//	        if(!$CommonModel->getOne($sql)){
//	           $this->error($GLOBALS['_LANG']['flow_no_shipping']);
//	        }
//	    }
//	    /* 订单中的总额 */
//	    $total 							= 	order_fee($order, $cart_goods, $consignee);
//	    $order['bonus']       		 	= 	$total['bonus'];
//	    $order['goods_amount'] 			= 	$total['goods_price'];
//	    $order['discount']     			= 	$total['discount'];
//	    $order['surplus']      			= 	$total['surplus'];
//	    $order['tax']          			=	$total['tax'];
//	
//	    // 购物车中的商品能享受红包支付的总额
//	    $discount_amout 				= 	compute_discount_amount();
//	    // 红包和积分最多能支付的金额为商品总额
//	    $temp_amout 					= 	$order['goods_amount'] - $discount_amout;
//	    if ($temp_amout <= 0){
//	        $order['bonus_id'] 			= 	0;
//	    }
//	
//	    /* 配送方式 */
//	    if ($order['shipping_id'] > 0){
//	        $shipping 					= 	shipping_info($order['shipping_id']);
//	        $order['shipping_name'] 	= 	addslashes($shipping['shipping_name']);
//	    }
//	    $order['shipping_fee'] 			= 	$total['shipping_fee'];
//	    $order['insure_fee']   			= 	$total['shipping_insure'];
//	
//	    /* 支付方式 */
//	    if ($order['pay_id'] > 0){
//	        $payment 					= 	payment_info($order['pay_id']);
//	        $order['pay_name'] 			= 	addslashes($payment['pay_name']);
//	    }
//	    $order['pay_fee'] 				= 	$total['pay_fee'];
//	    $order['cod_fee'] 				= 	$total['cod_fee'];
//	
//	    /* 商品包装 */
//	    if ($order['pack_id'] > 0){
//	        $pack               		= 	pack_info($order['pack_id']);
//	        $order['pack_name'] 		= 	addslashes($pack['pack_name']);
//	    }
//	    $order['pack_fee'] 				= 	$total['pack_fee'];
//	
//	    /* 祝福贺卡 */
//	    if ($order['card_id'] > 0){
//	        $card               		= 	card_info($order['card_id']);
//	        $order['card_name'] 		= 	addslashes($card['card_name']);
//	    }
//	    $order['card_fee']      		= 	$total['card_fee'];
//	
//	    $order['order_amount']  		= 	number_format($total['amount'], 2, '.', '');
//	
//	    /* 如果全部使用余额支付，检查余额是否足够 */
//	    if ($payment['pay_code'] == 'balance' && $order['order_amount'] > 0){
//	        if($order['surplus'] >0){ //余额支付里如果输入了一个金额
//	            $order['order_amount'] 	= 	$order['order_amount'] + $order['surplus'];
//	            $order['surplus'] 		= 	0;
//	        }
//	        if ($order['order_amount'] > ($user_info['user_money'] + $user_info['credit_line'])){
//	            $this->error($GLOBALS['_LANG']['balance_not_enough']);
//	        }
//	        else{
//	            $order['surplus'] 		= 	$order['order_amount'];
//	            $order['order_amount'] 	= 	0;
//	        }
//	    }
//	
//	    /* 如果订单金额为0（使用余额或积分或红包支付），修改订单状态为已确认、已付款 */
//	    if ($order['order_amount'] <= 0){
//	        $order['order_status'] 		= 	OS_CONFIRMED;
//	        $order['confirm_time'] 		= 	gmtime();
//	        $order['pay_status']   		= 	PS_PAYED;
//	        $order['pay_time']     		= 	gmtime();
//	        $order['order_amount'] 		= 	0;
//	    }
//	
//	    $order['integral_money']   		=	$total['integral_money'];
//	    $order['integral']         		= 	$total['integral'];
//	
//	    if ($order['extension_code'] == 'exchange_goods'){
//	        $order['integral_money']  	= 	0;
//	        $order['integral']         	= 	$total['exchange_integral'];
//	    }
//	
//	    $order['from_ad']          		= 	!empty($_SESSION['from_ad']) ? $_SESSION['from_ad'] : '0';
//	    $order['referer']          		= 	!empty($_SESSION['referer']) ? addslashes($_SESSION['referer']) : '';
//	
//	    /* 记录扩展信息 */
//	    if ($flow_type != CART_GENERAL_GOODS){
//	        $order['extension_code'] 	= 	$_SESSION['extension_code'];
//	        $order['extension_id'] 		= 	$_SESSION['extension_id'];
//	    }
//	
//	    $affiliate = unserialize($GLOBALS['_CFG']['affiliate']);
//	    if(isset($affiliate['on']) && $affiliate['on'] == 1 && $affiliate['config']['separate_by'] == 1){
//	        //推荐订单分成
//	        $parent_id 					= 	get_affiliate();
//	        if($user_id == $parent_id){
//	            $parent_id 				= 	0;
//	        }
//	    }
//	    elseif(isset($affiliate['on']) && $affiliate['on'] == 1 && $affiliate['config']['separate_by'] == 0){
//	        //推荐注册分成
//	        $parent_id 					= 	0;
//	    }
//	    else{
//	        //分成功能关闭
//	        $parent_id 					= 	0;
//	    }
//	    $order['parent_id'] 			= 	$parent_id;
//	
//	    /* 插入订单表 */
//	    $error_no 						= 	0;
//	    
//		// 获取新订单号
//        $order['order_sn'] 				= 	get_order_sn();
//        // 订单入库
//        $CommonModel->autoExecute( 'order_info' , $order, 'INSERT');
//
//        if($CommonModel->getError()){
//        	$this->error($CommonModel->getError());	
//        }
//        
//	
//	    $new_order_id 					= 	$CommonModel->insert_id();
//	    	    
//	    $order['order_id']	 			=	$new_order_id;
//	
//	    /* 插入订单商品 */
//	    $sql 							= 	"INSERT INTO " . 'ybr_order_goods' . "( " .
//							                "order_id, goods_id, goods_name, goods_sn, product_id, goods_number, market_price, ".
//							                "goods_price, goods_attr, is_real, extension_code, parent_id, is_gift, goods_attr_id) ".
//								            " SELECT '$new_order_id', goods_id, goods_name, goods_sn, product_id, goods_number, market_price, ".
//								           	"goods_price, goods_attr, is_real, extension_code, parent_id, is_gift, goods_attr_id".
//								            " FROM " .'ybr_cart' .
//								            " WHERE session_id = '".SESS_ID."' AND rec_type = '$flow_type'";
//	    
//	    
//	    
//	    t($sql);
//	    
//	    
//	    
//	    $CommonModel->query($sql);
//	    /* 修改拍卖活动状态 */
//	    if ($order['extension_code']=='auction'){
//	        $sql 						= 	"UPDATE ". 'ybr_goods_activity' ." SET is_finished='2' WHERE act_id=".$order['extension_id'];
//	        $CommonModel->query($sql);
//	    }
//	
//	    /* 处理余额、积分、红包 */
//	    if ($order['user_id'] > 0 && $order['surplus'] > 0){
//	        log_account_change($order['user_id'], $order['surplus'] * (-1), 0, 0, 0, sprintf($GLOBALS['_LANG']['pay_order'], $order['order_sn']));
//	    }
//	    if ($order['user_id'] > 0 && $order['integral'] > 0){
//	        log_account_change($order['user_id'], 0, 0, 0, $order['integral'] * (-1), sprintf($GLOBALS['_LANG']['pay_order'], $order['order_sn']));
//	    }
//	
//	
//	    if ($order['bonus_id'] > 0 && $temp_amout > 0){
//	        use_bonus($order['bonus_id'], $new_order_id);
//	    }
//	
//	    /* 如果使用库存，且下订单时减库存，则减少库存 */
//	    if ($GLOBALS['_CFG']['use_storage'] == '1' && $GLOBALS['_CFG']['stock_dec_time'] == SDT_PLACE){
//	        change_order_goods_storage($order['order_id'], true, SDT_PLACE);
//	    }
//	
//	    /* 给商家发邮件 */
//	    /* 增加是否给客服发送邮件选项 
//	    if ($GLOBALS['_CFG']['send_service_email'] && $GLOBALS['_CFG']['service_email'] != '')
//	    {
//	        $tpl = get_mail_template('remind_of_new_order');
//	        $this->assign('order', $order);
//	        $this->assign('goods_list', $cart_goods);
//	        $this->assign('shop_name', $GLOBALS['_CFG']['shop_name']);
//	        $this->assign('send_date', date($GLOBALS['_CFG']['time_format']));
//	        $content = $this->fetch('str:' . $tpl['template_content']);
//	        send_mail($GLOBALS['_CFG']['shop_name'], $GLOBALS['_CFG']['service_email'], $tpl['template_subject'], $content, $tpl['is_html']);
//	    }*/
//	
//	    /* 如果需要，发短信
//	    if ($GLOBALS['_CFG']['sms_order_placed'] == '1' && $GLOBALS['_CFG']['sms_shop_mobile'] != ''){
//	        include_once('includes/cls_sms.php');
//	        $sms = new sms();
//	        $msg = $order['pay_status'] == PS_UNPAYED ?
//	            $GLOBALS['_LANG']['order_placed_sms'] : $GLOBALS['_LANG']['order_placed_sms'] . '[' . $GLOBALS['_LANG']['sms_paid'] . ']';
//	        $sms->send($GLOBALS['_CFG']['sms_shop_mobile'], sprintf($msg, $order['consignee'], $order['tel']),'', 13,1);
//	    }*/
//	
//	    /* 如果订单金额为0 处理虚拟卡 */
//	    if ($order['order_amount'] <= 0){
//	        $sql 					= 	"SELECT goods_id, goods_name, goods_number AS num FROM ".
//						               	'ybr_cart' .
//						                " WHERE is_real = 0 AND extension_code = 'virtual_card'".
//						                " AND session_id = '".SESS_ID."' AND rec_type = '$flow_type'";
//	
//	        $res 					= 	$CommonModel->query($sql);
//	
//	        $virtual_goods 			= 	array();
//	        foreach ($res AS $row){
//	            $virtual_goods['virtual_card'][] 		= 	array('goods_id' => $row['goods_id'], 'goods_name' => $row['goods_name'], 'num' => $row['num']);
//	        }
//	
//	        if ($virtual_goods AND $flow_type != CART_GROUP_BUY_GOODS){
//	            /* 虚拟卡发货 */
//	            if (virtual_goods_ship($virtual_goods,$msg, $order['order_sn'], true)){
//	                /* 如果没有实体商品，修改发货状态，送积分和红包 */
//	                $sql 			= 	"SELECT COUNT(*)" .
//				                        " FROM " . 'ybr_order_goods' .
//				                        " WHERE order_id = '$order[order_id]' " .
//				                        " AND is_real = 1";
//	                if ($CommonModel->getOne($sql) <= 0){
//	                    /* 修改订单状态 */
//	                    update_order($order['order_id'], array('shipping_status' => SS_SHIPPED, 'shipping_time' => gmtime()));
//	
//	                    /* 如果订单用户不为空，计算积分，并发给用户；发红包 */
//	                    if ($order['user_id'] > 0){
//	                        /* 取得用户信息 */
//	                        $user 	= 	user_info($order['user_id']);
//	
//	                        /* 计算并发放积分 */
//	                        $integral 	= 	integral_to_give($order);
//	                        log_account_change($order['user_id'], 0, 0, intval($integral['rank_points']), intval($integral['custom_points']), sprintf($GLOBALS['_LANG']['order_gift_integral'], $order['order_sn']));
//	
//	                        /* 发放红包 */
//	                        send_order_bonus($order['order_id']);
//	                    }
//	                }
//	            }
//	        }
//	
//	    }
//	
//	    /* 清空购物车 */
//	    clear_cart($flow_type);
//	    /* 清除缓存，否则买了商品，但是前台页面读取缓存，商品数量不减少 */
//	    //clear_all_files();
//	
//	    /* 插入支付日志 */
//	    $order['log_id'] 		= 	insert_pay_log($new_order_id, $order['order_amount'], PAY_ORDER);
//	
//	    /* 取得支付信息，生成支付代码 
//	    if ($order['order_amount'] > 0){
//	        $payment 			= 	payment_info($order['pay_id']);	
//	        //include_once('includes/modules/payment/' . $payment['pay_code'] . '.php');
//	
//	        $pay_obj    		= 	new $payment['pay_code'];
//	
//	        $pay_online 		= 	$pay_obj->get_code($order, unserialize_config($payment['pay_config']));
//	
//	        $order['pay_desc'] 	= 	$payment['pay_desc'];
//	
//	        $this->assign('pay_online', $pay_online);
//	    }
//	    
//	    if(!empty($order['shipping_name'])){
//	        $order['shipping_name']		=	trim(stripcslashes($order['shipping_name']));
//	    }
//	    */
//	
//	    /* 订单信息 */
//	    $this->assign('order',      $order);
//	    $this->assign('total',      $total);
//	    $this->assign('goods_list', $cart_goods);
//	    $this->assign('order_submit_back', sprintf($GLOBALS['_LANG']['order_submit_back'], $GLOBALS['_LANG']['back_home'], $GLOBALS['_LANG']['goto_user_center'])); // 返回提示
//	
//	    
//	    t($_SESSION);
//	    
//	    
//   		user_uc_call('add_feed', array($order['order_id'], BUY_GOODS)); //推送feed到uc
//	    unset($_SESSION['flow_consignee']); // 清除session中保存的收货人信息
//	    unset($_SESSION['flow_order']);
//	    unset($_SESSION['direct_shopping']);
		
		$this->display('done');
	}
	
	
	/**
	 * 将选择的商品套餐放进购物车里面
	 * Enter description here ...
	 */
	public function add_to_cart(){
		
		$params				=	$_POST['goods'];
	
	    if (!empty($params['goods_id']) && empty($_POST['goods'])){
	        if (!is_numeric($params['goods_id']) || intval($params['goods_id']) <= 0){
	            //ecs_header("Location:./\n");
	            $this->ajaxReturn('error');
	        }
	        $goods_id 		= 	intval($params['goods_id']);
	        exit;
	    }
	
	    $result 			= 	array('error' => 0, 'message' => '', 'content' => '', 'goods_id' => '');
	    
	    if (empty($_POST['goods'])){
	        $result['error'] 	= 	1;
	        $this->ajaxReturn($result);
	    }
	
	    //$goods 				= 	$json->decode($_POST['goods']);
	    $goods				=	$params;
	
	    /* 检查：如果商品有规格，而post的数据没有规格，把商品的规格属性通过JSON传到前台 */
	    if (empty($goods['spec']) AND empty($goods['quick'])){
	        $sql 			= 	"SELECT a.attr_id, a.attr_name, a.attr_type, ".
	            				"g.goods_attr_id, g.attr_value, g.attr_price " .
	        					'FROM ' . 'ybr_goods_attr' . ' AS g ' .
	        					'LEFT JOIN ' . 'ybr_attribute' . ' AS a ON a.attr_id = g.attr_id ' .
	        					"WHERE a.attr_type != 0 AND g.goods_id = '" . $goods['goods_id'] . "' " .
	        					'ORDER BY a.sort_order, g.attr_price, g.goods_attr_id';
	
	        $res 			= 	M()->query($sql);
	
	        if (!empty($res)){
	            $spe_arr 	= 	array();
	            foreach ($res AS $row){
	                $spe_arr[$row['attr_id']]['attr_type'] 	= 	$row['attr_type'];
	                $spe_arr[$row['attr_id']]['name']     	= 	$row['attr_name'];
	                $spe_arr[$row['attr_id']]['attr_id']    = 	$row['attr_id'];
	                $spe_arr[$row['attr_id']]['values'][] 	= 	array(
	                                                            	'label'        => $row['attr_value'],
		                                                            'price'        => $row['attr_price'],
		                                                            'format_price' => price_format($row['attr_price'], false),
		                                                            'id'           => $row['goods_attr_id']
	                											);
	            }
	            $i 			= 	0;
	            $spe_array 	= 	array();
	            foreach ($spe_arr AS $row){
	                $spe_array[]	=	$row;
	            }
	            $result['error']   	= 	ERR_NEED_SELECT_ATTR;
	            $result['goods_id'] = 	$goods['goods_id'];
	            $result['parent'] 	= 	$goods['parent'];
	            $result['message'] 	= 	$spe_array;
	
	            $this->ajaxReturn($result);
	        }
	    }
	
	    /* 更新：如果是一步购物，先清空购物车 */
	    if ($GLOBALS['_CFG']['one_step_buy'] == '1'){
	        //clear_cart();
	    }
	
	    /* 检查：商品数量是否合法 */
	    if (!is_numeric($goods['number']) || intval($goods['number']) <= 0){
	        $result['error']   	= 	1;
	        $result['message'] 	= 	$GLOBALS['_LANG']['invalid_number'];
	    }
	    /* 更新：购物车 */
	    else{
	        // 更新：添加到购物车
	        if (addto_cart($goods['goods_id'], $goods['number'], $goods['spec'], $goods['parent'])){
	            if ($GLOBALS['_CFG']['cart_confirm'] > 2){
	                $result['message'] 	= 	'';
	            }
	            else{
	                $result['message'] 	= 	$GLOBALS['_CFG']['cart_confirm'] == 1 ? $GLOBALS['_LANG']['addto_cart_success_1'] : $GLOBALS['_LANG']['addto_cart_success_2'];
	            }
	
	            $result['content'] 		= 	insert_cart_info();
	            $result['one_step_buy'] = 	$GLOBALS['_CFG']['one_step_buy'];
	        }
	        else{
	            //$result['message']  	= 	$err->last_message();
	            //$result['error']    	= 	$err->error_no;
	            $result['goods_id'] 	= 	stripslashes($goods->goods_id);
	            if (is_array($goods->spec)){
	                $result['product_spec'] 	= 	implode(',', $goods->spec);
	            }
	            else{
	                $result['product_spec'] 	= 	$goods->spec;
	            }
	        }
	    }
	
	    $result['confirm_type'] = !empty($GLOBALS['_CFG']['cart_confirm']) ? $GLOBALS['_CFG']['cart_confirm'] : 2;
    	
    	$this->ajaxReturn($result);
	}
	
  
    /**
     * 添加收获地址
     * Enter description here ...
     */
    public function add_consignee_ok(){
		
    	$user_id		=	$_SESSION['user_id'];
    	
	    $address = array(
	        'user_id'    	=> $user_id,
	        'address_id' 	=> intval($_POST['address_id']),
	        'country'    	=> isset($_POST['country'])   ? intval($_POST['country'])  : 0,
	        'province'   	=> isset($_POST['province'])  ? intval($_POST['province']) : 0,
	        'city'       	=> isset($_POST['city'])      ? intval($_POST['city'])     : 0,
	        'district'   	=> isset($_POST['district'])  ? intval($_POST['district']) : 0,
	        'address'    	=> isset($_POST['address'])   ? compile_str(trim($_POST['address']))    : '',
	        'consignee'  	=> isset($_POST['consignee']) ? compile_str(trim($_POST['consignee']))  : '',
	        'email'      	=> isset($_POST['email'])     ? compile_str(trim($_POST['email']))      : '',
	        'tel'        	=> isset($_POST['tel'])       ? compile_str(make_semiangle(trim($_POST['tel']))) : '',
	        'mobile'     	=> isset($_POST['mobile'])    ? compile_str(make_semiangle(trim($_POST['mobile']))) : '',
	        'best_time'  	=> isset($_POST['best_time']) ? compile_str(trim($_POST['best_time']))  : '',
	        'sign_building' => isset($_POST['sign_building']) ? compile_str(trim($_POST['sign_building'])) : '',
	        'zipcode'       => isset($_POST['zipcode'])       ? compile_str(make_semiangle(trim($_POST['zipcode']))) : '',
	    );
		
	    $address['default']	=	1;
	    
	    if (update_address($address)){
	    	if($this->isAjax()){
	    		$template	=	'Flow:m_address_list';
	    		
	    		/* 获得用户所有的收货人信息 */
			    $consignee_list 			=	get_consignee_list($_SESSION['user_id']);   
			    	    
			    foreach($consignee_list as $k => $consignee){
			    	$consignee['key']		=	$k;
			    	$consignee_list[$k]		=	$consignee;
			    }	    
			    //if (count($consignee_list) < 5 && $_SESSION['user_id'] > 0){
			        ///* 如果用户收货人信息的总数小于5 则增加一个新的收货人信息 */
			        //$consignee_list[] 		= 	array('country' => $GLOBALS['_CFG']['shop_country'], 'email' => isset($_SESSION['email']) ? $_SESSION['email'] : '' ,'key'=>count($consignee_list) );
			    //}	    
			    //取得国家列表，如果有收货人列表，取得省市区列表
			    foreach ($consignee_list AS $region_id => $consignee){
			        //$consignee['country']  					= 	isset($consignee['country'])  ? intval($consignee['country'])  : 0;
			        $consignee['province'] 					= 	isset($consignee['province']) ? intval($consignee['province']) : 0;
			        $consignee['city']     					= 	isset($consignee['city'])     ? intval($consignee['city'])     : 0;
			        $consignee['district']     				= 	isset($consignee['district'])     ? intval($consignee['district'])     : 0;
					
			        //$consignee_list[$region_id]['country']	=	get_region_name($consignee['country']);
			        $consignee_list[$region_id]['province']	=	get_region_name($consignee['province']);
			        $consignee_list[$region_id]['city']		=	get_region_name($consignee['city']);
			        $consignee_list[$region_id]['district']	=	get_region_name($consignee['district']);
			    }		
			    // 默认的收获地址
			    $default_consignee 							= 	get_consignee($_SESSION['user_id']);
			    
			    $this->assign('consignee_list',$consignee_list);
			    $this->assign('default_consignee',$default_consignee); // 默认
	    		
	    		$html										=	$this->fetch($template);	    		
	    		$this->ajaxReturn($html);
	    	}	    	
	        //show_message(, $_LANG['address_list_lnk'], 'user.php?act=address_list');
	        $this->success( $GLOBALS['_LANG']['edit_address_success'],u('user/address_list') );
	    }
    }
	
    /**
     * 处理提交订单
     * Enter description here ...
     */
    public function commitorder(){    	
    	if(M()->autoCheckToken($_POST)){    		
    		echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">';    		
    		$CommonModel			=	new CommonModel();		
		    /* 取得购物类型 */
		    $flow_type 				= 	isset($_SESSION['flow_type']) ? intval($_SESSION['flow_type']) : CART_GENERAL_GOODS;
		    /* 检查购物车中是否有商品 */
		    $sql 					= 	"SELECT COUNT(*) FROM " . 'ybr_cart' .
		        						" WHERE session_id = '" . SESS_ID . "' " .
		        						"AND parent_id = 0 AND is_gift = 0 AND rec_type = '$flow_type'";
		    if ($CommonModel->getOne($sql) == 0){
		        $this->error($GLOBALS['_LANG']['no_goods_in_cart']);
		    }
		    /* 检查商品库存 */
		    /* 如果使用库存，且下订单时减库存，则减少库存 */
		    if ($GLOBALS['_CFG']['use_storage'] == '1' && $GLOBALS['_CFG']['stock_dec_time'] == SDT_PLACE){
		        $cart_goods_stock 			= 	get_cart_goods();
		        $_cart_goods_stock 			= 	array();
		        foreach ($cart_goods_stock['goods_list'] as $value){
		            $_cart_goods_stock[$value['rec_id']] 	= 	$value['goods_number'];
		        }
		        flow_cart_stock($_cart_goods_stock);
		        unset($cart_goods_stock, $_cart_goods_stock);
		    }
		    /*
		     * 检查用户是否已经登录
		     * 如果用户已经登录了则检查是否有默认的收货地址
		     * 如果没有登录则跳转到登录和注册页面
		     */
		    if (empty($_SESSION['direct_shopping']) && $_SESSION['user_id'] == 0){
		        /* 用户没有登录且没有选定匿名购物，转向到登录页面 */
		        $this->redirect('/pass/login');
		    }
			
		    /* 地址取向,新的或默认 */
		    $address_id							=	empty($_POST['address_id']) ? 0 : intval($_POST['address_id']);
		    if($address_id){
		    	$consignee						=	M('UserAddress')->where("address_id='{$address_id}'")->find();
		    }
		    else{
		    	$consignee 						= 	get_consignee($_SESSION['user_id']);
		    }
		    
		    //t($consignee);
		    
		    /* 检查收货人信息是否完整 */
		    if (!check_consignee_info($consignee, $flow_type)){
		        /* 如果不完整则转向到收货人信息填写界面 */
		        $this->redirect('/user/address_list','',2,'您的收获地址信息不全,请补充完整....');
		    }
		
		    $_POST['how_oos'] 			= 	isset($_POST['how_oos']) ? intval($_POST['how_oos']) : 0;
		    $_POST['card_message'] 		= 	isset($_POST['card_message']) ? compile_str($_POST['card_message']) : '';
		    $_POST['inv_type'] 			= 	!empty($_POST['inv_type']) ? compile_str($_POST['inv_type']) : '';
		    $_POST['inv_payee'] 		= 	isset($_POST['inv_payee']) ? compile_str($_POST['inv_payee']) : '';
		    $_POST['inv_content'] 		= 	isset($_POST['inv_content']) ? compile_str($_POST['inv_content']) : '';
		    $_POST['postscript'] 		= 	isset($_POST['postscript']) ? compile_str($_POST['postscript']) : '';
		
		    $order = array(
		        'shipping_id'     => empty($_POST['shipping']) ? 0 : intval($_POST['shipping']) , // 快递方式
		        'pay_id'          => intval($_POST['payment']),
		        'pack_id'         => isset($_POST['pack']) ? intval($_POST['pack']) : 0,
		        'card_id'         => isset($_POST['card']) ? intval($_POST['card']) : 0,
		        'card_message'    => trim($_POST['card_message']),
		        'surplus'         => isset($_POST['surplus']) ? floatval($_POST['surplus']) : 0.00,
		        'integral'        => isset($_POST['integral']) ? intval($_POST['integral']) : 0,
		        'bonus_id'        => isset($_POST['bonus']) ? intval($_POST['bonus']) : 0,
		        'need_inv'        => empty($_POST['need_inv']) ? 0 : 1,
		        'inv_type'        => $_POST['inv_type'],
		        'inv_payee'       => trim($_POST['inv_payee']),
		        'inv_content'     => $_POST['inv_content'],
		        'postscript'      => trim($_POST['postscript']),
		        'how_oos'         => isset($GLOBALS['_LANG']['oos'][$_POST['how_oos']]) ? addslashes($GLOBALS['_LANG']['oos'][$_POST['how_oos']]) : '',
		        'need_insure'     => isset($_POST['need_insure']) ? intval($_POST['need_insure']) : 0,
		        'user_id'         => $_SESSION['user_id'],
		        'add_time'        => gmtime(),
		        'order_status'    => OS_CONFIRMED,
		        'shipping_status' => SS_UNSHIPPED,
		        'pay_status'      => PS_UNPAYED,
		        'agency_id'       => get_agency_by_regions(array($consignee['country'], $consignee['province'], $consignee['city'], $consignee['district']))
		        );
				        
		    /* 扩展信息 */
		    if (isset($_SESSION['flow_type']) && intval($_SESSION['flow_type']) != CART_GENERAL_GOODS){
		        $order['extension_code'] 	= 	$_SESSION['extension_code'];
		        $order['extension_id'] 		= 	$_SESSION['extension_id'];
		    }
		    else{
		        $order['extension_code'] 	= 	'';
		        $order['extension_id'] 		= 	0;
		    }
		    
		    /* 检查积分余额是否合法 */
		    $user_id 						= 	$_SESSION['user_id'];
		    if ($user_id > 0){
		        $user_info 					= 	user_info($user_id);
		
		        $order['surplus'] 			= 	min($order['surplus'], $user_info['user_money'] + $user_info['credit_line']);
		        if ($order['surplus'] < 0){
		            $order['surplus'] 		= 	0;
		        }
		
		        // 查询用户有多少积分
		        $flow_points 				= 	flow_available_points();  // 该订单允许使用的积分
		        $user_points 				= 	$user_info['pay_points']; // 用户的积分总数
		
		        $order['integral'] 			= 	min($order['integral'], $user_points, $flow_points);
		        if ($order['integral'] < 0){
		            $order['integral'] 		= 	0;
		        }
		    }
		    else{
		        $order['surplus']  			= 	0;
		        $order['integral'] 			= 	0;
		    }
		
		    /* 检查红包是否存在 */
		    if ($order['bonus_id'] > 0){
		        $bonus 						= 	bonus_info($order['bonus_id']);
		
		        if (empty($bonus) || $bonus['user_id'] != $user_id || $bonus['order_id'] > 0 || $bonus['min_goods_amount'] > cart_amount(true, $flow_type)){
		            $order['bonus_id'] 		= 	0;
		        }
		    }
		    elseif (isset($_POST['bonus_sn'])){
		        $bonus_sn 					= 	trim($_POST['bonus_sn']);
		        $bonus 						= 	bonus_info(0, $bonus_sn);
		        $now 						= 	gmtime();
		        if (empty($bonus) || $bonus['user_id'] > 0 || $bonus['order_id'] > 0 || $bonus['min_goods_amount'] > cart_amount(true, $flow_type) || $now > $bonus['use_end_date'])
		        {}
		        else{
		            if ($user_id > 0){
		                $sql 				= 	"UPDATE " . 'ybr_user_bonus' . " SET user_id = '$user_id' WHERE bonus_id = '$bonus[bonus_id]' LIMIT 1";
		                $CommonModel->query($sql);
		            }
		            $order['bonus_id'] 		= 	$bonus['bonus_id'];
		            $order['bonus_sn'] 		= 	$bonus_sn;
		        }
		    }
		
		    /* 订单中的商品 */
		    $cart_goods 					= 	cart_goods($flow_type);
		    
		
		    if (empty($cart_goods)){
		        $this->error($GLOBALS['_LANG']['no_goods_in_cart']);
		    }
		
		    /* 检查商品总额是否达到最低限购金额 */
		    if ($flow_type == CART_GENERAL_GOODS && cart_amount(true, CART_GENERAL_GOODS) < $GLOBALS['_CFG']['min_goods_amount']){
		        $this->error( sprintf($GLOBALS['_LANG']['goods_amount_not_enough'], price_format($GLOBALS['_CFG']['min_goods_amount'], false)) );
		    }
		
		    /* 收货人信息 */
		    foreach ($consignee as $key => $value){
		        $order[$key] 				= 	addslashes($value);
		    }
		
		   /* 判断是不是实体商品 */
		    foreach ($cart_goods AS $val){
		        /* 统计实体商品的个数 */
		        if ($val['is_real']){
		            $is_real_good			=	1;
		        }
		    }
		    /* 快递的方式
		     * if(isset($is_real_good)){
		        $sql						=	"SELECT shipping_id FROM " . 'ybr_shipping' . " WHERE shipping_id=".$order['shipping_id'] ." AND enabled =1"; 
		        if(!$CommonModel->getOne($sql)){
		           	$this->error($GLOBALS['_LANG']['flow_no_shipping']);
		        }
		    }*/
		    /* 订单中的总额 */
		    $total 							= 	order_fee($order, $cart_goods, $consignee);
		    $order['bonus']       		 	= 	$total['bonus'];
		    $order['goods_amount'] 			= 	$total['goods_price'];
		    $order['discount']     			= 	$total['discount'];
		    $order['surplus']      			= 	$total['surplus'];
		    $order['tax']          			=	$total['tax'];
		
		    // 购物车中的商品能享受红包支付的总额
		    $discount_amout 				= 	compute_discount_amount();
		    // 红包和积分最多能支付的金额为商品总额
		    $temp_amout 					= 	$order['goods_amount'] - $discount_amout;
		    if ($temp_amout <= 0){
		        $order['bonus_id'] 			= 	0;
		    }
		
		    /* 配送方式 */
		    if ($order['shipping_id'] > 0){
		        $shipping 					= 	shipping_info($order['shipping_id']);
		        $order['shipping_name'] 	= 	addslashes($shipping['shipping_name']);
		    }
		    $order['shipping_fee'] 			= 	$total['shipping_fee'];
		    $order['insure_fee']   			= 	$total['shipping_insure'];
		
		    /* 支付方式 : 只支持货到付款  */
		    if ($order['pay_id'] > 0){
		        $payment 					= 	payment_info($order['pay_id']);		        
		        $order['pay_name'] 			= 	addslashes($payment['pay_name']);
		    }
		    $order['pay_fee'] 				= 	$total['pay_fee'];
		    $order['cod_fee'] 				= 	$total['cod_fee'];
		
		    /* 商品包装 */
		    if ($order['pack_id'] > 0){
		        $pack               		= 	pack_info($order['pack_id']);
		        $order['pack_name'] 		= 	addslashes($pack['pack_name']);
		    }
		    $order['pack_fee'] 				= 	$total['pack_fee'];
		
		    /* 祝福贺卡 */
		    if ($order['card_id'] > 0){
		        $card               		= 	card_info($order['card_id']);
		        $order['card_name'] 		= 	addslashes($card['card_name']);
		    }
		    $order['card_fee']      		= 	$total['card_fee'];
		
		    $order['order_amount']  		= 	number_format($total['amount'], 2, '.', '');
		
		    /* 如果全部使用余额支付，检查余额是否足够 */
		    if ($payment['pay_code'] == 'balance' && $order['order_amount'] > 0){
		        if($order['surplus'] >0){ //余额支付里如果输入了一个金额
		            $order['order_amount'] 	= 	$order['order_amount'] + $order['surplus'];
		            $order['surplus'] 		= 	0;
		        }
		        if ($order['order_amount'] > ($user_info['user_money'] + $user_info['credit_line'])){
		            $this->error($GLOBALS['_LANG']['balance_not_enough']);
		        }
		        else{
		            $order['surplus'] 		= 	$order['order_amount'];
		            $order['order_amount'] 	= 	0;
		        }
		    }
		
		    /* 如果订单金额为0（使用余额或积分或红包支付），修改订单状态为已确认、已付款 */
		    if ($order['order_amount'] <= 0){
		        $order['order_status'] 		= 	OS_CONFIRMED;
		        $order['confirm_time'] 		= 	gmtime();
		        $order['pay_status']   		= 	PS_PAYED;
		        $order['pay_time']     		= 	gmtime();
		        $order['order_amount'] 		= 	0;
		    }
		
		    $order['integral_money']   		=	$total['integral_money'];
		    $order['integral']         		= 	$total['integral'];
		
		    if ($order['extension_code'] == 'exchange_goods'){
		        $order['integral_money']  	= 	0;
		        $order['integral']         	= 	$total['exchange_integral'];
		    }
		
		    $order['from_ad']          		= 	!empty($_SESSION['from_ad']) ? $_SESSION['from_ad'] : '0';
		    $order['referer']          		= 	!empty($_SESSION['referer']) ? addslashes($_SESSION['referer']) : '';
		
		    /* 记录扩展信息 */
		    if ($flow_type != CART_GENERAL_GOODS){
		        $order['extension_code'] 	= 	$_SESSION['extension_code'];
		        $order['extension_id'] 		= 	$_SESSION['extension_id'];
		    }
		
		    $affiliate = unserialize($GLOBALS['_CFG']['affiliate']);
		    if(isset($affiliate['on']) && $affiliate['on'] == 1 && $affiliate['config']['separate_by'] == 1){
		        //推荐订单分成
		        $parent_id 					= 	get_affiliate();
		        if($user_id == $parent_id){
		            $parent_id 				= 	0;
		        }
		    }
		    elseif(isset($affiliate['on']) && $affiliate['on'] == 1 && $affiliate['config']['separate_by'] == 0){
		        //推荐注册分成
		        $parent_id 					= 	0;
		    }
		    else{
		        //分成功能关闭
		        $parent_id 					= 	0;
		    }
		    $order['parent_id'] 			= 	$parent_id;
		
		    /* 插入订单表 */
		    $error_no 						= 	0;
		    
			// 获取新订单号
	        $order['order_sn'] 				= 	get_order_sn();
	        // 订单入库
	        $CommonModel->autoExecute( 'order_info' , $order, 'INSERT');
	
	        if($CommonModel->getError()){
	        	$this->error($CommonModel->getError());	
	        }
	    
		    $new_order_id 					= 	$CommonModel->insert_id();		    	    
		    $order['order_id']	 			=	$new_order_id;
		
		    /* 插入订单商品 */
		    $sql 							= 	"INSERT INTO " . 'ybr_order_goods' . "( " .
								                "order_id, goods_id, goods_name, goods_sn, product_id, goods_number, market_price, ".
								                "goods_price, goods_attr, is_real, extension_code, parent_id, is_gift, goods_attr_id) ".
									            " SELECT '$new_order_id', goods_id, goods_name, goods_sn, product_id, goods_number, market_price, ".
									           	"goods_price, goods_attr, is_real, extension_code, parent_id, is_gift, goods_attr_id".
									            " FROM " .'ybr_cart' .
									            " WHERE session_id = '".SESS_ID."' AND rec_type = '$flow_type'";
		    
		    $CommonModel->query($sql);
		    /* 修改拍卖活动状态 */
		    if ($order['extension_code']=='auction'){
		        $sql 						= 	"UPDATE ". 'ybr_goods_activity' ." SET is_finished='2' WHERE act_id=".$order['extension_id'];
		        $CommonModel->query($sql);
		    }
		
		    /* 处理余额、积分、红包 */
		    if ($order['user_id'] > 0 && $order['surplus'] > 0){
		        log_account_change($order['user_id'], $order['surplus'] * (-1), 0, 0, 0, sprintf($GLOBALS['_LANG']['pay_order'], $order['order_sn']));
		    }
		    if ($order['user_id'] > 0 && $order['integral'] > 0){
		        log_account_change($order['user_id'], 0, 0, 0, $order['integral'] * (-1), sprintf($GLOBALS['_LANG']['pay_order'], $order['order_sn']));
		    }
		
		
		    if ($order['bonus_id'] > 0 && $temp_amout > 0){
		        use_bonus($order['bonus_id'], $new_order_id);
		    }
		
		    /* 如果使用库存，且下订单时减库存，则减少库存 */
		    if ($GLOBALS['_CFG']['use_storage'] == '1' && $GLOBALS['_CFG']['stock_dec_time'] == SDT_PLACE){
		        change_order_goods_storage($order['order_id'], true, SDT_PLACE);
		    }
		
//		    /* 给商家发邮件 */
//		    /* 增加是否给客服发送邮件选项 
//		    if ($GLOBALS['_CFG']['send_service_email'] && $GLOBALS['_CFG']['service_email'] != '')
//		    {
//		        $tpl = get_mail_template('remind_of_new_order');
//		        $this->assign('order', $order);
//		        $this->assign('goods_list', $cart_goods);
//		        $this->assign('shop_name', $GLOBALS['_CFG']['shop_name']);
//		        $this->assign('send_date', date($GLOBALS['_CFG']['time_format']));
//		        $content = $this->fetch('str:' . $tpl['template_content']);
//		        send_mail($GLOBALS['_CFG']['shop_name'], $GLOBALS['_CFG']['service_email'], $tpl['template_subject'], $content, $tpl['is_html']);
//		    }*/
//		
//		    /* 如果需要，发短信
//		    if ($GLOBALS['_CFG']['sms_order_placed'] == '1' && $GLOBALS['_CFG']['sms_shop_mobile'] != ''){
//		        include_once('includes/cls_sms.php');
//		        $sms = new sms();
//		        $msg = $order['pay_status'] == PS_UNPAYED ?
//		            $GLOBALS['_LANG']['order_placed_sms'] : $GLOBALS['_LANG']['order_placed_sms'] . '[' . $GLOBALS['_LANG']['sms_paid'] . ']';
//		        $sms->send($GLOBALS['_CFG']['sms_shop_mobile'], sprintf($msg, $order['consignee'], $order['tel']),'', 13,1);
//		    }*/
//		
		    /* 如果订单金额为0 处理虚拟卡 */
//		    if ($order['order_amount'] <= 0){
//		        $sql 					= 	"SELECT goods_id, goods_name, goods_number AS num FROM ".
//							               	'ybr_cart' .
//							                " WHERE is_real = 0 AND extension_code = 'virtual_card'".
//							                " AND session_id = '".SESS_ID."' AND rec_type = '$flow_type'";
//		
//		        $res 					= 	$CommonModel->query($sql);
//		
//		        $virtual_goods 			= 	array();
//		        foreach ($res AS $row){
//		            $virtual_goods['virtual_card'][] 		= 	array('goods_id' => $row['goods_id'], 'goods_name' => $row['goods_name'], 'num' => $row['num']);
//		        }
//		
//		        if ($virtual_goods AND $flow_type != CART_GROUP_BUY_GOODS){
//		            /* 虚拟卡发货 */
//		            if (virtual_goods_ship($virtual_goods,$msg, $order['order_sn'], true)){
//		                /* 如果没有实体商品，修改发货状态，送积分和红包 */
//		                $sql 			= 	"SELECT COUNT(*)" .
//					                        " FROM " . 'ybr_order_goods' .
//					                        " WHERE order_id = '$order[order_id]' " .
//					                        " AND is_real = 1";
//		                if ($CommonModel->getOne($sql) <= 0){
//		                    /* 修改订单状态 */
//		                    update_order($order['order_id'], array('shipping_status' => SS_SHIPPED, 'shipping_time' => gmtime()));
//		
//		                    /* 如果订单用户不为空，计算积分，并发给用户；发红包 */
//		                    if ($order['user_id'] > 0){
//		                        /* 取得用户信息 */
//		                        $user 	= 	user_info($order['user_id']);
//		
//		                        /* 计算并发放积分 */
//		                        $integral 	= 	integral_to_give($order);
//		                        log_account_change($order['user_id'], 0, 0, intval($integral['rank_points']), intval($integral['custom_points']), sprintf($GLOBALS['_LANG']['order_gift_integral'], $order['order_sn']));
//		
//		                        /* 发放红包 */
//		                        send_order_bonus($order['order_id']);
//		                    }
//		                }
//		            }
//		        }
//		
//		    }

		
		    /* 清空购物车*/ 
		    clear_cart($flow_type);
		    // 清除缓存，否则买了商品，但是前台页面读取缓存，商品数量不减少 
		    //clear_all_files();

		    /* 插入支付日志 */
		    $order['log_id'] 		= 	insert_pay_log($new_order_id, $order['order_amount'], PAY_ORDER);
		
		    /* 取得支付信息，生成支付代码 
		    if ($order['order_amount'] > 0){
		        $payment 			= 	payment_info($order['pay_id']);	
		        //include_once('includes/modules/payment/' . $payment['pay_code'] . '.php');
		
		        $pay_obj    		= 	new $payment['pay_code'];
		
		        $pay_online 		= 	$pay_obj->get_code($order, unserialize_config($payment['pay_config']));
		
		        $order['pay_desc'] 	= 	$payment['pay_desc'];
		
		        $this->assign('pay_online', $pay_online);
		    }		    
		    if(!empty($order['shipping_name'])){
		        $order['shipping_name']		=	trim(stripcslashes($order['shipping_name']));
		    }
		    */
		
		    /* 订单信息 */
		    $this->assign('order',      $order);
		    $this->assign('total',      $total);
		    $this->assign('goods_list', $cart_goods);
		    $this->assign('order_submit_back', sprintf($GLOBALS['_LANG']['order_submit_back'], $GLOBALS['_LANG']['back_home'], $GLOBALS['_LANG']['goto_user_center'])); // 返回提示

	   		/*user_uc_call('add_feed', array($order['order_id'], BUY_GOODS)); //推送feed到uc*/
		    unset($_SESSION['flow_consignee']); 	// 清除session中保存的收货人信息
		    unset($_SESSION['flow_order']);
		    unset($_SESSION['direct_shopping']);  	
		    /*跳转到支付页面*/ 
		    $this->redirect('/flow/payorder',array( 'id'=>$order['order_id'] ),2,'订单提交成功,正在为您跳转到订单列表...');		   
    	}
    	else{
    		$this->redirect('/flow','',2,'同一个订单不能提交两次');
    	}
    }
	
    /*预约界面*/
    public function yuyue(){		
    	$this->display();
    }
    
    /*更新购物车*/
    public function update_cart(){
    	/**处理字符串*/
    	if(empty($_POST['datas'])){
    		$this->ajaxReturn(1,"",1);
    	}
    	$array			=	explode("_", trim($_POST['datas']));
    	$arr[$array[0]]	=	$array[1];    	
    	
	    if (isset($arr) && is_array($arr)){
	        flow_update_cart($arr);
	    }
	    $this->ajaxReturn(1,"",1);
    }
    
    public function payorder(){
    	$order_id		=	$this->_get('id');
    	if(empty($order_id)){
    		$msg_list	=	build_tips_msg(QS_ORDER_EMPTY);
    		$this->assign("msg_list",$msg_list);
    		$this->display('msg');
    		exit();
    	}  
    	$OrderModel		=	new OrderModel();
    	$PaymentModel	=	new PaymentModel();
		//订单信息
    	$order_info		=	$OrderModel->get_order_detail_byid($order_id);
    	
    	if(empty($order_info)){
    		$msg_list	=	build_tips_msg(QS_ORDER_EMPTY);
    		$this->assign("msg_list",	$msg_list);
    		$this->display('msg');
    		exit();
    	}
		//订单是否支付
    	if($order_info['order_status'] == OS_CONFIRMED && $order_info['pay_status'] == PS_UNPAYED){//未支付
    		//不处理
    	}
    	elseif($order_info['order_status'] == OS_CONFIRMED && $order_info['pay_status'] == PS_PAYING){//支付中
    		/*$msg_list	=	build_tips_msg(QS_PAYING);
    		$this->assign("msg_list",	$msg_list);
    		$this->display('msg');
    		exit();*/
    	}
    	elseif($order_info['order_status'] == OS_CONFIRMED && $order_info['pay_status'] == PS_PAYED){//已支付  		
    		$msg_list	=	build_tips_msg(QS_PAYED);
    		$this->assign("msg_list",	$msg_list);
    		$this->display('msg');
    		exit();
    	}    	
    	//取出系统安装的支付方式
    	$payment_list	=	$PaymentModel->get_payment_list();
		$this->assign("payment_list",	$payment_list);
		$this->assign("order_info",		$order_info);
    	$this->display();
    }
    
    public function set_pay_method(){    	
    	$order_id		=	intval($_REQUEST['order_id']);
    	$pay_id			=	intval($_REQUEST['pay_id']);    	
    	$pay_method		=	trim($_REQUEST['type']);
    	$payment		=	array();    	
    	$PaymentModel	=	new PaymentModel();    	
    	//用户是否已经登录	  
	    if (empty($_SESSION['direct_shopping']) && $_SESSION['user_id'] == 0){
	    	header('Location:'  . U('pass/login') . "?back_url=" . $_COOKIE['back_url'] );
	    }	
	    //订单号        
	    if(empty($order_id)){
	    	$this->ajaxReturn(1,$GLOBALS['_LANG']['not_exists_order_sn'],1);
	    }	 
	    //支付方式是否存在并且可使用
	    $payment_info	=	$PaymentModel->get_payment_byid($pay_id);	    
	    if(empty($payment_info)){
	    	$this->ajaxReturn(1,$GLOBALS['_LANG']['not_exists_payment'],1);
	    }	    
	    if($payment_info['enabled'] == 1 || $payment_info['enabled'] == true){
	    	//加载支付插件
	    	vendor("Payment." . $pay_method);	
	    	if(class_exists($pay_method)){    		
	    		$_payment_obj		=  & new $pay_method ();
	    		//获取订单信息
    			$order_info			=	M('OrderInfo')->where("order_id = '{$order_id}'")->find();	
				//生成支付连接
	    		$url				=	$_payment_obj->get_code($order_info,$payment_info);
	    		$this->ajaxReturn(1,$url,1);    		
	    	}
	    	else{
	    		$this->ajaxReturn(1,$GLOBALS['_LANG']['payment_system_error'],1);
	    	}	
	    }
	    else{
	    	$this->ajaxReturn(1,$GLOBALS['_LANG']['payment_not_enabled'],1);
	    }	    
    }
    
    /**
     * 去付款
     * 
     */
    public function gotopay(){    	
    	$order_id		=	intval($_REQUEST['order_id']);
    	$pay_id			=	intval($_REQUEST['pay_id']);    	
    	$pay_method		=	trim($_REQUEST['type']);
    	$payment		=	array();    	
    	$PaymentModel	=	new PaymentModel();    	
    	//用户是否已经登录	  
	    if (empty($_SESSION['direct_shopping']) && $_SESSION['user_id'] == 0){
	    	header('Location:'  . U('pass/login') . "?back_url=" . $_COOKIE['back_url'] );
	    }	
	    //订单号        
	    if(empty($order_id)){
	    	$this->ajaxReturn(1,$GLOBALS['_LANG']['not_exists_order_sn'],1);
	    }
	    //判断该订单是否已经支付
	    
	    //支付方式是否存在并且可使用
	    $payment_info	=	$PaymentModel->get_payment_byid($pay_id);	    
	    if(empty($payment_info)){
	    	$this->ajaxReturn(1,$GLOBALS['_LANG']['not_exists_payment'],1);
	    }	    
	    if($payment_info['enabled'] == 1 || $payment_info['enabled'] == true){
	    	//加载支付插件
	    	vendor("Payment." . $pay_method);	
	    	if(class_exists($pay_method)){    		
	    		$_payment_obj		=  & new $pay_method ();
	    		//获取订单信息
    			$order_info			=	M('OrderInfo')->where("order_id = '{$order_id}'")->find();	
				//生成支付连接
	    		$pay_form			=	$_payment_obj->get_code($order_info,$payment_info);
				$template	=	"Flow:paytpl";						
				$html		=	$this->fetch($template);
				echo $html . $pay_form;
	    	}
	    	else{
	    		//$this->ajaxReturn(1,$GLOBALS['_LANG']['payment_system_error'],1);
	    	}	
	    }
	    else{
	    	//$this->ajaxReturn(1,$GLOBALS['_LANG']['payment_not_enabled'],1);
	    }	  
    }
    
    /**
     * 统一跳转到支付成功页面
     * 
     */
    public function pay_success(){    	
    	$order_id		=	intval($_REQUEST['order_id']);
    	$pay_id			=	intval($_REQUEST['payment_type']); //识别支付方式的pay_id
    	$PaymentModel	=	new PaymentModel();    	
    	$OrderModel		=	new OrderModel();    	
		/*
	     * 检查用户是否已经登录
	     * 如果用户已经登录了则检查是否有默认的收货地址
	     * 如果没有登录则跳转到登录和注册页面
	     */    	
	    if (empty($_SESSION['direct_shopping']) && $_SESSION['user_id'] == 0){
	        /* 用户没有登录且没有选定匿名购物，转向到登录页面 */
	    	header('Location:'  . U('pass/login') . "?back_url=" . $_COOKIE['back_url'] );
	    }
	    //识别两种验证支付结果:1,支付直接跳转;2,支付后,点击支付成功按钮
	    if(empty($order_id)){
		    //支付方式
		    $payment	=	$PaymentModel->get_payment_byid($pay_id);
		    $pay_method	=	$payment['pay_code'];			
		    //导入相应的支付插件
	    	vendor("Payment." . $pay_method);    	
	    	if(class_exists($pay_method)){
	    		$_payment_obj		=  	& new $pay_method ();
				//获取支付结果
	    		$pay_result			=	$_payment_obj->respond();
	    		if($pay_result === true){    			   		
	    			$msg_list	=	build_tips_msg(QS_PAY_SUCCESS);
	    		}
	    		else{
	    			$msg_list	=	build_tips_msg($msg_type);
	    		}    		
	    	}
	    	else{
	    		$msg_list	=	build_tips_msg($msg_type);
	    	}  
	    	$this->assign("msg_list",$msg_list);
	    	$this->display('msg');	      	
	    }
	    else{
	    	$order_info		=	order_info($order_id);
	    	$msg_list		=	array();	    	
	    	if($order_info['order_status'] == OS_CONFIRMED && $order_info['pay_status'] == PS_PAYED){//支付成功
    			$msg_list	=	build_tips_msg(QS_PAY_SUCCESS);
	    	}
	    	elseif($order_info['order_status'] == OS_CONFIRMED && $order_info['pay_status'] == PS_PAYING){//付款中
	    		$msg_list	=	build_tips_msg(QS_PAYING);		    		
	    	}
	    	elseif($order_info['order_status'] == OS_CONFIRMED && $order_info['pay_status'] == PS_UNPAYED){//未付款
	    		$msg_list	=	build_tips_msg(QS_UNPAYED);	 
	    	}
	    	$this->assign("msg_list",$msg_list);
	    	$this->display('msg');	   
	    }
    }
    
    public function ajax_check_cart(){
    	$datas	=	check_cart();    	
    	//t($datas);
    	$this->ajaxReturn(1,$datas,1);
    }
    
}




;